/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.mqdemo3;

import cn.ac.iie.di.datadock.rdata.exchange.client.core.REFieldType;
import cn.ac.iie.di.datadock.rdata.exchange.client.exception.REConnectionException;
import cn.ac.iie.di.datadock.rdata.exchange.client.exception.RESessionException;
import cn.ac.iie.di.datadock.rdata.exchange.client.v1.connection.REConnection;
import cn.ac.iie.di.datadock.rdata.exchange.client.v1.session.RESendSession;
import cn.ac.iie.di.datadock.rdata.exchange.client.v1.session.RESendSessionBuilder;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.math3.random.RandomDataGenerator;
 
/**
 *发送消息，接收消息后，更改字段内容转发给新的消息队列
 * @author dell
 */
public class Sender {
//    public static final String NAME_SERVER= "192.168.203.128:9879";
    
    public static final String NAME_SERVER= "192.168.11.95:6666";
    public static final String TOPIC="zhj_commentUpdate";
    public static void main(String[] args) throws REConnectionException, RESessionException {
        //建链接
        REConnection conn=new REConnection(NAME_SERVER);
        RESendSessionBuilder builder=(RESendSessionBuilder)conn.getSendSessionBuilder(TOPIC);
        //建立字段
        builder.addColumn("postchannel", REFieldType.String, true);
        builder.addColumn("postid",REFieldType.String,true);
        builder.addColumn("msg_type",REFieldType.String,true);
        builder.addColumn("domain", REFieldType.String, true);
        builder.addColumn("likenum",REFieldType.Int,true);
        builder.addColumn("key", REFieldType.String, true);
//        HashMap<String,REFieldType> map=new HashMap();
//        map.put("long1", REFieldType.Long);
//        map.put("str1",REFieldType.String);
//        REFieldType.Structs.setDetail("structsTest", map);
        RESendSession session=null;
        try {
            session=(RESendSession)builder.build();
            session.start();
        } catch (RESessionException ex) {
            ex.printStackTrace();
        }
        int count=0;
        //给字段赋值
        while(true){
            //5条消息一打包
            for(int i=0;i<20;i++){
                
                session.setString("postchannel", "123456");
                session.setString("postid",getRandomString(4));
                session.setString("msg_type", getRandomString(5));
                session.setString("domain", getRandomString(6));
                session.setInt("likenum",new RandomDataGenerator().nextInt(1,5));
                session.setString("key", getRandomString(7));
//                Map[]maps=new HashMap[3];
//                for(int j=0;j<3;j++){
//                    HashMap<String,Object>map1=new HashMap<>();
//                    map1.put("ldsadsa", 111L);
//                    map1.put("fsfds",j+"heihei");
//                    maps[j]=map1;
//                };
//                session.setStructs("structsTest", maps);
                session.add();
                
            }
            //发送缓存
            
            session.flush();
            count++;
            System.out.println("message"+count);
            try {
                Thread.sleep(5000);
            } catch (InterruptedException ex) {
                Logger.getLogger(Sender.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    public static String getRandomString(int length) { //length表示生成字符串的长度  
    String base = "abcdefghijklmnopqrstuvwxyz0123456789";     
   Random random = new Random();     
   StringBuffer sb = new StringBuffer();     
   for (int i = 0; i < length; i++) {     
        int number = random.nextInt(base.length());     
       sb.append(base.charAt(number));     
    }     
    return sb.toString();     
 } 

}
